# $Id: joingrdialog.tcl 1821 2009-07-30 22:19:37Z sergei $

#set gr_nick $user
set gr_group "talks"
set gr_server conference.jabber.org
set gr_passwd ""

custom::defvar gr_nick_list {} \
        [::msgcat::mc "Join group dialog data (nicks)."] -group Hidden
custom::defvar gr_group_list {} \
        [::msgcat::mc "Join group dialog data (groups)."] -group Hidden
custom::defvar gr_server_list {} \
        [::msgcat::mc "Join group dialog data (servers)."] -group Hidden

proc ecursor_entry {e} {
    $e icursor end
}

proc pack_input {fr row var lab args} {

    label $fr.l$var -text $lab
    ecursor_entry [eval entry $fr.$var -textvar $var $args]

    grid $fr.l$var -row $row -column 0 -sticky e
    grid $fr.$var  -row $row -column 1 -sticky ew
}

proc pack_combo {fr row var lab args} {

    label $fr.l$var -text $lab
    ecursor_entry [eval ComboBox $fr.$var -textvariable $var $args].e

    grid $fr.l$var -row $row -column 0 -sticky e
    grid $fr.$var  -row $row -column 1 -sticky ew
}

proc update_combo_list {list entry num} {

    set ind [lsearch -exact $list $entry]
    if {$ind >= 0} {
	set newlist [linsert [lreplace $list $ind $ind] 0 $entry]
    } else {
	set newlist [linsert $list 0 $entry]
    }
    if {[llength $newlist] > $num} {
	return [lreplace $newlist end end]
    } else {
	return $newlist
    }
}

proc join_group_dialog {xlib args} {
    global gr_nick gr_group gr_server gr_xlib gr_passwd
    global gr_nick_list gr_group_list gr_server_list

    if {[llength [connections]] == 0} return

    set gr_passwd ""
    foreach {opt val} $args {
	switch -- $opt {
	    -server     {set server $val}
	    -group      {set group $val}
	    -nick       {set nick $val}
	    -password   {set gr_passwd $val}
	}
    }

    set gw .joingroup
    catch { destroy $gw }

    Dialog $gw -title [::msgcat::mc "Join group"] -separator 1 -anchor e \
	    -default 0 -cancel 1 -modal none

    set gf [$gw getframe]
    grid columnconfigure $gf 1 -weight 1

    if {[info exists group]} {
	set gr_group $group
#	set gr_group_list [update_combo_list $gr_group_list $gr_group 20]
    } elseif {[llength $gr_group_list]} {
	set gr_group [lindex $gr_group_list 0]
    }
    if {[info exists server]} {
	set gr_server $server
#	set gr_server_list [update_combo_list $gr_server_list $gr_server 10]
    } elseif {[llength $gr_server_list]} {
	set gr_server [lindex $gr_server_list 0]
    }
    if {[info exists nick]} {
	set gr_nick $nick
#	set gr_nick_list [update_combo_list $gr_nick_list $gr_nick 10]
    } else {
	set gr_nick [get_group_nick [::xmpp::jid::jid $gr_group $gr_server] $gr_nick]
    }
    if {$xlib == ""} {
	set xlib [lindex [connections] 0]
    }
    set gr_xlib [connection_jid $xlib]

    pack_combo $gf 0 gr_group [::msgcat::mc "Group:"] -values $gr_group_list
    pack_combo $gf 1 gr_server [::msgcat::mc "Server:"] -values $gr_server_list
    pack_combo $gf 2 gr_nick [::msgcat::mc "Nick:"] -values $gr_nick_list
    pack_input $gf 3 gr_passwd [::msgcat::mc "Password:"] -width 30 -show *

    if {[llength [connections]] > 1} {
	foreach c [connections] {
	    lappend connections [connection_jid $c]
	}
	pack_combo $gf 5 gr_xlib [::msgcat::mc "Connection:"] \
	    -values $connections
    }

    $gw add -text [::msgcat::mc "Join"] -command [list join_group1 $gw]
	    
    $gw add -text [::msgcat::mc "Cancel"] -command [list destroy $gw]

    $gw draw $gf.gr_group
}

proc join_group1 {gw} {
    global gr_nick gr_group gr_server gr_xlib gr_passwd
    global gr_nick_list gr_group_list gr_server_list

    destroy $gw

    set gr_nick_list [update_combo_list $gr_nick_list $gr_nick 10]
    set gr_group_list [update_combo_list $gr_group_list $gr_group 20]
    set gr_server_list [update_combo_list $gr_server_list $gr_server 10]

    foreach c [connections] {
	if {[connection_jid $c] == $gr_xlib} {
	    set xlib $c
	}
    }
    if {![info exists xlib]} {
	set xlib [lindex [connections] 0]
    }

    join_group $xlib [::xmpp::jid::jid $gr_group $gr_server] \
	       -nick $gr_nick \
	       -password $gr_passwd
}

proc join_group {xlib jid args} {
    global gr_nick

    set password ""
    foreach {opt val} $args {
	switch -- $opt {
	    -password   { set password $val }
	    -nick       { set nick $val }
	}
    }
    if {$xlib == ""} {
	set xlib [lindex [connections] 0]
    }
    if {![info exists nick]} {
	set nick [get_group_nick $jid $gr_nick]
    }

    set group [xmpp::jid::normalize $jid]
    muc::join_group $xlib $group $nick $password

    chat::activate [chat::chatid $xlib $group]
}

proc set_our_groupchat_nick {group nick} {
    global groupchats

    debugmsg conference "SET NICK: $group '$nick'"

    set group [::xmpp::jid::normalize $group]
    set groupchats(nick,$group) $nick
}

proc get_our_groupchat_nick {group} {
    global groupchats
    
    debugmsg conference "GET NICK: $group"

    set group [::xmpp::jid::normalize $group]
    return $groupchats(nick,$group)
}

###############################################################################

proc joingroup_disco_node_menu_setup {m bw tnode data parentdata} {
    lassign $data type xlib jid node
    switch -- $type {
	item -
	item2 {
	    set identities [::disco::browser::get_identities $bw $tnode]

	    if {[lempty $identities]} {
		set identities [::disco::browser::get_parent_identities $bw $tnode]
	    }

	    # JID with resource is not a room JID
	    if {[::xmpp::jid::resource $jid] != ""} return

	    foreach id $identities {
		if {[::xmpp::xml::getAttr $id category] == "conference"} {
		    $m add command -label [::msgcat::mc "Join group..."] \
			-command [list join_group_dialog $xlib \
				       -server [::xmpp::jid::server $jid] \
				       -group [::xmpp::jid::node $jid]]
		    break
		}
	    }
	}
    }
}

hook::add disco_node_menu_hook \
	  [namespace current]::joingroup_disco_node_menu_setup 45

###############################################################################

# vim:ts=8:sw=4:sts=4:noet
